package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.dto.OrderReportDto;
import com.sky.dto.TurnOverReportDto;
import com.sky.dto.UserReportDto;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.*;
import com.sky.service.ReportService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zcf
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private DishMapper dishMapper;

    /**
     * 查看今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime, LocalDateTime endTime) {
        //2.获取新增用户数
        List<UserReportDto> list = userMapper.list3(beginTime, endTime);
        List<Integer> collect = list.stream().map(
                UserReportDto::getCount
        ).collect(Collectors.toList());
        Integer newUsers = collect.stream().reduce(0, Integer::sum);

        //3.获取有效订单总数
        List<OrderReportDto> list1 = orderMapper.countOrders(beginTime, endTime, 5);
        List<Integer> collect1 = list1.stream().map(
                OrderReportDto::getCount
        ).collect(Collectors.toList());
        Integer validOrderCount = collect1.stream().reduce(0, Integer::sum);

        //4.获取完成率
        List<OrderReportDto> list2 = orderMapper.countOrders(beginTime, endTime, null);
        List<Integer> collect2 = list2.stream().map(
                OrderReportDto::getCount
        ).collect(Collectors.toList());
        Integer totalOrderCount = collect2.stream().reduce(0, Integer::sum);
        Double orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;

        //5.获取营业额
        List<TurnOverReportDto> list3 = reportMapper.list(beginTime, endTime, 5);
        List<BigDecimal> bigDecimals = list3.stream().map(
                TurnOverReportDto::getOrderMoney
        ).collect(Collectors.toList());
        BigDecimal decimal = bigDecimals.stream().reduce(new BigDecimal("0"), BigDecimal::add);
        Double doubleValue = decimal.doubleValue();
        //6.获取平均客单价
        Double unitPrice = doubleValue / validOrderCount.doubleValue();
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .turnover(doubleValue)
                .unitPrice(unitPrice)
                .build();
    }


    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //1.查询停售套餐数量
        Integer count = setmealMapper.getByStatus(StatusConstant.DISABLE);
        //2.查询起售套餐数量
        Integer sold = setmealMapper.getByStatus(StatusConstant.ENABLE);

        return SetmealOverViewVO.builder().discontinued(count).sold(sold).build();
    }


    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer count = dishMapper.getByStatus(StatusConstant.DISABLE);
        Integer sold = dishMapper.getByStatus(StatusConstant.ENABLE);
        return DishOverViewVO.builder().discontinued(count).sold(sold).build();
    }


    /**
     * 查询订单总览
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        //1.获取全部订单
        Integer allOrders = orderMapper.count1();
        //2.获取待派送订单
        Integer deliveredOrders = orderMapper.count(Orders.CONFIRMED);
        //3.获取待接单
        Integer waitingOrders = orderMapper.count(Orders.TO_BE_CONFIRMED);
        //4.获取已完成
        Integer completedOrders = orderMapper.count(Orders.COMPLETED);
        //5.获取已取消
        Integer cancelledOrders = orderMapper.count(Orders.CANCELLED);
        return OrderOverViewVO.builder().cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .allOrders(allOrders).waitingOrders(waitingOrders).build();
    }
}
